﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using ClassGenerator.Lib;

namespace ClassGeneratorConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Table newTables = new Table();
            List<Table> tables = newTables.getAllTablesAndColumns();

            CreatePOCOClass(tables, "Project.Entities");
            //CresteClass(tables);

            Console.WriteLine("Terminou...");
            Console.ReadLine();
        }

        static void CreatePOCOClass(List<Table> tables, string classNamespace)
        {
            string poco = string.Empty;
            string[] fields = new string[5000];
            int startPosition = 0;
            string currentTable = string.Empty;
            int nextTable = 0;
            string classFolder = "C:\\ClassesProjeto\\" + classNamespace;

            tables.ForEach(delegate(Table table) {

                if (tables.IndexOf(table).Equals(tables.Count - 1))
                {
                    fields[startPosition + 1] = "\t}";
                    fields[startPosition + 2] = "}";

                    if (!Directory.Exists(classFolder))
                        Directory.CreateDirectory(classFolder);

                    File.WriteAllLines(classFolder + "\\" + currentTable + ".cs", fields);
                }
                
                if (!table.TABLE_NAME.Equals(currentTable))
                {
                    nextTable++;

                    if (nextTable > 1)
                    {
                        fields[startPosition + 1] = "\t}";
                        fields[startPosition + 2] = "}";

                        if (!Directory.Exists(classFolder))
                            Directory.CreateDirectory(classFolder);

                        File.WriteAllLines(classFolder + "\\" + currentTable + ".cs", fields);

                        fields = new string[5000];
                    }
                    
                    
                    fields[0] = "using System;";
                    fields[1] = "using System.Collections.Generic;";
                    fields[2] = "using System.Linq;";
                    fields[3] = "";
                    fields[4] = "";
                    fields[5] = "namespace " + classNamespace;
                    fields[6] = "{";
                    fields[7] = "\tpublic class " + table.TABLE_NAME;
                    fields[8] = "\t{";

                    startPosition = 9;
                }


                poco += "\t\tpublic ";

                if (table.DATA_TYPE.Equals("varchar") || table.DATA_TYPE.Equals("text"))
                    poco += "string";
                else if (table.DATA_TYPE.Equals("int") || table.DATA_TYPE.Equals("tinyint"))
                    poco += "int";
                else if (table.DATA_TYPE.Equals("bit"))
                    poco += "bool";
                else if (table.DATA_TYPE.Equals("datetime"))
                    poco += "DateTime";
                else if (table.DATA_TYPE.Equals("smalldatetime"))
                    poco += "DateTime";

                if (table.IS_NULLABLE.Equals("YES") && (!table.DATA_TYPE.Equals("varchar") || !table.DATA_TYPE.Equals("text")))
                    poco += "? ";
                else
                    poco += " ";

                poco += table.COLUMN_NAME + " { get; set; }";
                fields[startPosition] = poco;
                startPosition++;
                poco = string.Empty;
                currentTable = table.TABLE_NAME;
            });
        }

        static void CresteClass(List<Table> tables)
        {
            Console.Write("Namespace:");
            string strNamespace = Console.ReadLine();
            
            string template = "using System;\r\n";
            template += "using System.Collections.Generic;\r\n";
            template += "using System.Linq;\r\n";
            template += "using System.Text;\r\n";
            template += "using System.Threading.Tasks;\r\n";
            template += "using System.Data;\r\n";
            template += "\r\n\r\n";

            if (!Directory.Exists("C:\\ClassesProjeto"))
                Directory.CreateDirectory("C:\\ClassesProjeto");

            File.WriteAllText("C:\\ClassesProjeto\\Teste.cs", template);
        }


    }
}
